package com.wfcm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.wfcm.annotation.IgnoreSign;
import com.wfcm.entity.*;
import com.wfcm.jedis.JedisResult;
import com.wfcm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wfcm.annotation.IgnoreToken;
import com.wfcm.utils.R;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * @content 课程分类Controller
 */
@RequestMapping("/indexRecommend")
@Controller
public class IndexRecommendController {
	private Logger log = LoggerFactory.getLogger(IndexRecommendController.class);
	@Autowired
	private WfIndexRecommendService indexRecommendService;
    @Autowired
    private WfIndexTypeService indexTypeService;
	@Autowired
	private WfCourseInfoService courseInfoService;
	@Autowired
	private DistributionService distributionService;
	@Autowired
	private WfChapterRecordService chapterRecordService;

	/**
	 * 首页推荐
	 */
	@RequestMapping("/indexType")
	@ResponseBody
	@IgnoreToken
	public R indexType() {

        List<WfIndexTypeEntity> indexTypeList =  indexTypeService.queryList(null);
		List<Map> resultList = new ArrayList<>();
        for (int i = 0; i < indexTypeList.size(); i++) {
			WfIndexTypeEntity indexType = indexTypeList.get(i);
			int num = 0;
			if(indexType.getIndexRecommend().getDisplaytype()!=2) {
				num=4;
			}else { num=1; }

			Map m = getIndexMap(indexType.getIndextypecode(),num,indexType.getIndextypename());
			resultList.add(m);
		}
		return R.ok().put("indexTypeList",resultList);
	}

	private Map getIndexMap(int indexTypeCode, int num, String typeName){
		Map<String,Object>  map = new HashMap<String,Object>();
		//10,免费课程  ； 11，精品课程； 12，超值课程包；22,数学分级课  ;23语文分级课； 24，音频推荐； 25，今日推荐
		map.put("indexTypeCode", indexTypeCode);
		int total = indexRecommendService.queryTotal(map);
		map.put("num", num);//首页分类展示数据的条数
		List <WfIndexRecommendEntity> indexList = new ArrayList<WfIndexRecommendEntity>();
		List <WfIndexRecommendEntity> resultList = new ArrayList<WfIndexRecommendEntity>();
		indexList = indexRecommendService.queryIndexList(map);
		for(int i=0 ; i<indexList.size(); i++) {
			WfIndexRecommendEntity indexRecommendEntity = indexList.get(i);
			if(1 == indexRecommendEntity.getBuytype()) {//按课程购买
				map.put("courseId",indexRecommendEntity.getCourseid() );
				//查出首页推荐类别 -- 查出对应的课程 -- 查出对应的讲师
				WfIndexRecommendEntity courseInfo = indexRecommendService.queryCourseInfo(map);
				if(null!=courseInfo){
					courseInfo.getCourseInfoEntity().setLabel(courseInfoService.queryCourseLabel(1, courseInfo.getCourseid()));
					resultList.add(courseInfo);
				}else {
					total --;
				}
			}else if(2 == indexRecommendEntity.getBuytype()){//按章节购买
				map.put("chapterId",indexRecommendEntity.getChapterid() );
				//查出首页推荐类别  -- 查出对应的章节-- 查出对应的课程  -- 查出对应的讲师
				WfIndexRecommendEntity courseChapter = indexRecommendService.queryCourseChapter(map);
				if(null!=courseChapter){
					//查询播放次数
					WfChapterRecordEntity chapterRecord = chapterRecordService.queryObject(indexRecommendEntity.getChapterid() );
					if(null != chapterRecord) {
						courseChapter.setSalecount(chapterRecord.getNumber());
					}
					courseChapter.getCourseChapterEntity().setLabel(courseInfoService.queryCourseLabel(2, courseChapter.getChapterid()));
					resultList.add(courseChapter);
				}else {
					total --;
				}
			}else if(3 == indexRecommendEntity.getBuytype()){//按课程包购买
				map.put("cardId",indexRecommendEntity.getCardid() );
				//查出首页推荐类别 -- 查出对应的课程包
				WfIndexRecommendEntity vipCard = indexRecommendService.querySimpleVipCard(map);
				vipCard.getVipCardEntity().setLabel(courseInfoService.queryCourseLabel(3, vipCard.getCardid()));
				resultList.add(vipCard);
			}
		}
		Map<String,Object>  resultMap = new HashMap<String,Object>();
		resultMap.put("resultList", resultList);
		resultMap.put("total",total);
		resultMap.put("typeName",typeName);
		return resultMap;
	}

	/**
	 * 推荐分类
	 */
	@RequestMapping("/indexTypeList")
	@ResponseBody
	@IgnoreToken
	public R findIndexType(HttpServletRequest req) {
		String itcString =  req.getParameter("indexTypeCode");
		Integer indexTypeCode  = Integer.valueOf(itcString);
		Map<String,Object>  map = new HashMap<String,Object>();//10,免费课程  ； 11，精品课程； 12，超值课程包；22,数学分级课  ;23语文分级课； 24，音频推荐； 25，今日推荐
		map.put("indexTypeCode", indexTypeCode);
		int total = indexRecommendService.queryTotal(map);
		List <WfIndexRecommendEntity> resultList = new ArrayList<>();
        WfIndexRecommendEntity info = null;
        List <WfIndexRecommendEntity>  indexList = indexRecommendService.queryIndexList(map);
		for(int i=0 ; i<indexList.size(); i++) {
			WfIndexRecommendEntity indexRecommendEntity = indexList.get(i);
			if(1 == indexRecommendEntity.getBuytype() && indexTypeCode != 12) {//按课程购买
				map.put("courseId",indexRecommendEntity.getCourseid() );
				//查出首页推荐类别 -- 查出对应的课程 -- 查出对应的讲师
                info = indexRecommendService.queryCourseInfo(map);
				if(null != info) {
					resultList.add(info);
				}else {
					total --;
				}
			}else if(2 == indexRecommendEntity.getBuytype() && indexTypeCode != 12){//按章节购买
				map.put("chapterId",indexRecommendEntity.getChapterid() );
				//查出首页推荐类别  -- 查出对应的章节-- 查出对应的课程  -- 查出对应的讲师
                info = indexRecommendService.queryCourseChapter(map);
				if(null != info) {
					resultList.add(info);
				}else {
					total --;
				}
			}else if(3 == indexRecommendEntity.getBuytype() && indexTypeCode != 10){//按课程包购买
				map.put("cardId",indexRecommendEntity.getCardid() );
				//查出首页推荐类别 -- 查出对应的课程包 -- 查出课程包+课程关系表 -- 查出对应的课程 -- 查出对应的讲师
                info = indexRecommendService.querySimpleVipCard(map);
                if(null != info) {
					Map<String, Object> csMap = new HashMap<>();
					List<WfCardCourseRelationEntity> cardCourseRelationList = info.getVipCardEntity().getCardCourseRelationList();
					for (int k = 0; k < cardCourseRelationList.size(); k++) {
						csMap.put("courseId", cardCourseRelationList.get(k).getCourseid());
						List<WfCourseInfoEntity> courseSpeakerList = courseInfoService.queryList(csMap);//查出对应的讲师
						cardCourseRelationList.get(k).setCourseInfoEntity(courseSpeakerList.get(0));
					}
					resultList.add(info);
				}else {
                    total --;
                }
			}
		}
		Map<String,Object>  resultMap = new HashMap<String,Object>();
		resultMap.put("resultList", resultList);
		resultMap.put("total",total);
		return R.ok(resultMap);
	}

	@IgnoreToken
	@ResponseBody
	@RequestMapping("globalParam")
	public R globalParam() {
		R r = R.ok();
		try {
			Distribution d1 = distributionService.queryDistributionByKey("h5_global_video_url");
			Distribution d2 = distributionService.queryDistributionByKey("ios_global_video_url");
			Distribution d3 = distributionService.queryDistributionByKey("android_global_video_url");
			Distribution d4 = distributionService.queryDistributionByKey("h5_global_img_url");
			Distribution d5 = distributionService.queryDistributionByKey("ios_global_img_url");
			Distribution d6 = distributionService.queryDistributionByKey("android_global_img_url");
			r.put("h5_video_url", d1 != null ? d1.getRemark() : "");
			r.put("ios_video_url", d2 != null ? d2.getRemark() : "");
			r.put("android_video_url", d3 != null ? d3.getRemark() : "");
			r.put("h5_img_url", d4 != null ? d4.getRemark() : "");
			r.put("ios_img_url", d5 != null ? d5.getRemark() : "");
			r.put("android_img_url", d6 != null ? d6.getRemark() : "");
		} catch (Exception e) {
			r = R.error(500, "服务器异常！");
			log.error(e.getMessage(), e);
		}
		return r;
	}


    /**
	 * 首页推荐Jedis
	 */
   /* @RequestMapping("/jedisIndexType")
	@ResponseBody
	@IgnoreToken
	@IgnoreSign
	public IndexTypeResult jedisIndexType() {
		return indexTypeService.jedisList();
	}*/

	/**
     * 删除首页推荐Jedis
     */
    @RequestMapping("/delIndexType")
    @ResponseBody
    @IgnoreToken
    public int delIndexType() {
        return indexTypeService.delIndexType();
    }

	/**
	 * 首页推荐Jedis
	 */
    /*@RequestMapping("/jedisIndexType")*/
	@RequestMapping("/indexComplete")
	@ResponseBody
	@IgnoreToken
	@IgnoreSign
	public JedisResult indexComplete(HttpServletRequest req) {
		return indexTypeService.indexComplete(req);
	}
}
